﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;

namespace BitsOfStuff.Support
{
    internal class MyMath
    {
        public static List<double> smooth(List<double> values, int precision)
        {
            return values.Select(value => Math.Round(value, precision)).ToList();
        }

        public static List<int> findPeaks(List<double> values, double minRecognizeValue)
        {
            var precision = minRecognizeValue.ToString(CultureInfo.InvariantCulture).Length - 2;
            var result = new List<int>();
            values = smooth(values, precision);
            var rising = !(values[1] > values[2]);
            for (int i = 1; i < values.Count - 1; i++)
            {
                if (rising)
                {
                    if ((values[i - 1] - values[i]) >= minRecognizeValue)
                    {
                        result.Add(i - 1);
                        rising = false;
                    }
                }
                else
                {
                    if ((values[i - 1] - values[i]) >= -minRecognizeValue)
                    {
                        result.Add(i - 1);
                        rising = true;
                    }
                }
            }
            return result;
        }
    }
}